package cn.fscode.code.generator.controller;

import cn.fscode.code.generator.model.po.DatasourceConfigPO;
import cn.fscode.code.generator.model.po.GenerateHistoryPO;
import cn.fscode.code.generator.model.po.TemplateConfigPO;
import cn.fscode.code.generator.model.req.GeneratorReq;
import cn.fscode.code.generator.model.resp.GenTableResp;
import cn.fscode.code.generator.model.resp.GenerateHistoryResp;
import cn.fscode.code.generator.service.DatasourceConfigService;
import cn.fscode.code.generator.service.GenerateHistoryService;
import cn.fscode.code.generator.service.IGenTableService;
import cn.fscode.code.generator.service.TemplateConfigService;
import cn.fscode.commons.core.response.Resp;
import cn.fscode.commons.tool.core.StringUtils;
import com.alibaba.fastjson2.JSON;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author shenguangyang
 */
@RestController
@RequestMapping("history")
@RequiredArgsConstructor
public class GenerateHistoryController {
    private final GenerateHistoryService generateHistoryService;
    private final DatasourceConfigService datasourceConfigService;
    private final IGenTableService genTableService;
    private final TemplateConfigService templateConfigService;

    /**
     * 查询所有记录
     *
     * @return 返回集合，没有返回空List
     */
    @RequestMapping("list")
    public Resp<List<GenerateHistoryResp>> listAll() {
        List<GenerateHistoryPO> generateHistories = generateHistoryService.listAll();
        List<GenerateHistoryResp> generateHistoryRespList = generateHistories.stream()
                .map(generateHistory -> {
                    GenerateHistoryResp generateHistoryResp = new GenerateHistoryResp();
                    GeneratorReq generatorReq = JSON.parseObject(generateHistory.getConfigContent(), GeneratorReq.class);
                    GenTableResp genTableResp = genTableService.getGenTableRespByTableId(generatorReq.getGenTableId());
                    if (genTableResp == null) {
                        return null;
                    }
                    DatasourceConfigPO datasourceConfigPO = datasourceConfigService.getById(genTableResp.getDatasourceId());
                    if (Objects.isNull(datasourceConfigPO)) {
                        return null;
                    }
                    List<String> templateNames = this.listTemplateNames(StringUtils.toList(generatorReq.getTemplateIds(), ",", Long.class));
                    generateHistoryResp.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(generateHistory.getCreateTime()));
                    generateHistoryResp.setConfigContent(generatorReq);
                    generateHistoryResp.setDatasource(datasourceConfigPO.getName());
                    generateHistoryResp.setTemplateNames(templateNames);
                    return generateHistoryResp;
                })
                .filter(Objects::nonNull)
                .collect(Collectors.toList());
        return Resp.ok(generateHistoryRespList);
    }

    private List<String> listTemplateNames(List<Long> idList) {
        return templateConfigService.listByIds(idList)
                .stream()
                .map(TemplateConfigPO::getName)
                .collect(Collectors.toList());
    }

}